<?php

function getWeekString($time)
{
    $weekArray=array("日","一","二","三","四","五","六");
    return $weekArray[date("w",$time)];
}

/**
 * @param string $timeOrDate
 * @return int
 */
function getWeekStart($timeOrDate = "")
{
    if (!$timeOrDate) {
        $timeOrDate = time();
    }
    if (is_string($timeOrDate)) {
        $time = strtotime($timeOrDate);
    } else {
        $time = (int) $timeOrDate;
    }
    return $time - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600;
}

/**
 * @param string $timeOrDate
 * @return int
 */
function getWeekEnd($timeOrDate = "")
{
    if (!$timeOrDate) {
        $timeOrDate = time();
    }
    if (is_string($timeOrDate)) {
        $time = strtotime($timeOrDate);
    } else {
        $time = (int) $timeOrDate;
    }
    return $time + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600;
}


/**
 * @param string $timeOrDate
 * @return int
 */
function getDayStart($timeOrDate = "")
{
    if (!$timeOrDate) {
        $timeOrDate = time();
    }
    if (is_string($timeOrDate)) {
        $time = strtotime($timeOrDate);
    } else {
        $time = (int) $timeOrDate;
    }
    return strtotime(date( "Y-m-d" , $time ));
}

/**
 * @param string $timeOrDate
 * @return int
 */
function getDayEnd($timeOrDate = "")
{
    if (!$timeOrDate) {
        $timeOrDate = time();
    }
    if (is_string($timeOrDate)) {
        $time = strtotime($timeOrDate);
    } else {
        $time = (int) $timeOrDate;
    }
    return strtotime(date( "Y-m-d" , $time )) + 24 * 3600 - 1;
}



/**
 * @param string $timeOrDate
 * @return int
 */
function getMonthStart($timeOrDate = "")
{
    if (!$timeOrDate) {
        $timeOrDate = time();
    }
    if (is_string($timeOrDate)) {
        $time = strtotime($timeOrDate);
    } else {
        $time = (int) $timeOrDate;
    }
    return strtotime(date('Y-m', $time) . '-01 00:00:00');
}

/**
 * @param string $timeOrDate
 * @return int
 */
function getMonthEnd($timeOrDate = "")
{
    if (!$timeOrDate) {
        $timeOrDate = time();
    }
    if (is_string($timeOrDate)) {
        $time = strtotime($timeOrDate);
    } else {
        $time = (int) $timeOrDate;
    }
    return strtotime(date('Y-m', $time) . '-' . date('t', time()) . ' 23:59:59');
}

function timeToYMDHIS($time = 0,$pos = "",$isEnd = 0)
{
    if (!$pos) {
        if (!$time) {
            $time = time();
        }
        return date("Y-m-d H:i:s",$time);
    }
    $isEnd = 1;
    if ($isEnd) {
        switch ($pos) {
            case "Y":
                $day = $time / (3600 * 24 * 365);
                return floor($day);
            case "m":
                $day = $time / (3600 * 24 * 30);
                return floor($day);
            case "d":
                $day = $time / (3600 * 24);
                return floor($day);
            case "H":
                $day = $time / (3600);
                return floor($day);
            case "i":
                $day = $time / (60);
                return floor($day);
        }
    }else{
        return (int)date("$pos",$time);
    }

}

function timeToWords($time,$min = "i")
{
    $str = "";
    $isEnd = 0;
    if ($num = timeToYMDHIS( $time , "Y"  ,$isEnd) - 1970) {
        $str = $str . $num."年";
        $isEnd = 0;
    }else{
        $isEnd = 1;
    }
    if ($min == "Y") {return $str;}
    if ($num = timeToYMDHIS( $time , "m"  ,$isEnd)) {
        $str = $str . $num."月";
        $isEnd = 0;
    }else{
        $isEnd = 1;
    }
    if ($min == "m") {return $str;}
    if ($num = timeToYMDHIS( $time , "d"  ,$isEnd)) {
        $str = $str . $num."天";
        $isEnd = 0;
    }else{
        $isEnd = 1;
    }
    if ($min == "d") {return $str;}
    if ($num = timeToYMDHIS( $time , "H"  ,$isEnd)) {
        $str = $str . $num."小时";
        $isEnd = 0;
    }else{
        $isEnd = 1;
    }
    if ($min == "H") {return $str;}
    if ($num = timeToYMDHIS( $time , "i"  ,$isEnd)) {
        $str = $str . $num."分钟";
        $isEnd = 0;
    }else{
        $isEnd = 1;
    }
    if ($min == "i") {return $str;}
    if ($num = timeToYMDHIS( $time , "s"  ,$isEnd)) {
        $str = $str . $num."秒";
        $isEnd = 0;
    }else{
        $isEnd = 1;
    }
    return $str;
}
